import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Dashboard from '../views/Dashboard.vue'
// 导入管理后台路由
import { routes as adminRoutes } from '../admin/router'

Vue.use(VueRouter)

// 解决Vue Router重复导航错误
// 获取原本的push方法
const originalPush = VueRouter.prototype.push
// 重写push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    // 如果是导航重复错误，则忽略
    if (err.name !== 'NavigationDuplicated') {
      // 如果是其他错误，则继续抛出
      return Promise.reject(err)
    }
    // 如果是导航重复，返回一个resolved的Promise
    return Promise.resolve()
  })
}

// 同样处理replace方法
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      return Promise.reject(err)
    }
    return Promise.resolve()
  })
}

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: Dashboard
  },
  {
    path: '/challenges',
    name: 'Challenges',
    component: () => import(/* webpackChunkName: "challenge-list" */ '../views/ChallengeList.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/challenge/:id',
    name: 'ChallengeDetail',
    component: () => import(/* webpackChunkName: "challenge-detail" */ '../views/Challenge.vue')
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/Profile.vue')
  },
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('../views/Settings.vue')
  },
  {
    path: '/points',
    name: 'Points',
    component: () => import('../views/Points.vue')
  },
  {
    path: '/knowledge',
    name: 'Knowledge',
    component: () => import('../views/Knowledge.vue')
  },
  {
    path: '/leaderboard',
    name: 'Leaderboard',
    component: () => import('../views/Leaderboard.vue')
  },
  {
    path: '/challenge-list',
    name: 'ChallengeList',
    component: () => import('../views/ChallengeList.vue')
  },
  {
    path: '/community',
    name: 'Community',
    component: () => import('../views/Community.vue')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/AboutPage.vue')
  },
  // 将所有管理后台路由添加到主路由中
  ...adminRoutes,
  {
    path: '*',
    redirect: '/'
  }
]

// 确保我们使用主应用的路由模式和基础URL
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 移除全局前置守卫，不再检查身份验证

export default router 